package cn.stylefeng.guns.modular.business.frequency.cmprovince.controller;

import cn.stylefeng.guns.core.enums.ReturnCode;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.modular.business.frequency.cmarea.entity.CmArea;
import cn.stylefeng.guns.modular.business.frequency.cmcity.entity.CmCity;
import cn.stylefeng.guns.modular.business.frequency.cmprovince.entity.CmProvince;
import cn.stylefeng.guns.modular.business.frequency.cmprovince.model.param.AdministrativeDivisionDTO;
import cn.stylefeng.guns.modular.business.frequency.cmprovince.model.result.PkgProvince;
import cn.stylefeng.guns.modular.business.frequency.cmprovince.service.CmProvinceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地址库：行政区划管理
 *
 * @Description:
 * @Author jzy
 * @Date 2021/7/5 0005
 **/
@Api(tags = "地址库：行政区划管理")
@RestController
@RequestMapping("/api/region")
public class AddressController {

    @Resource
    private CmProvinceService provinceService;

    @ApiOperation(value = "城市分页列表")
    @GetMapping
    public ResponseData getCityListPage(AdministrativeDivisionDTO administrativeDivisionDTO) {

        return provinceService.getCityListPage(administrativeDivisionDTO);
    }

    @ApiOperation(value = "所有城市列表")
    @GetMapping("/all")
    @SuppressWarnings("unchecked")
    public ResponseData getAllCityList() {
        return provinceService.getAllCityList();

    }

    // TODO: 2023/7/3 返回app端 省市区树状结构数据
    @ApiOperation(value = "所有城市列表")
    @GetMapping("/allTree")
    @SuppressWarnings("unchecked")
    public ResponseData getTreeAllCityList() {
        return provinceService.getTreeAllCityList();
    }

    @ApiOperation(value = "获取非县级市区列表")
    @GetMapping("/{cityCode}/area")
    public ResponseData getAreaListByCityCode(@PathVariable String cityCode) {
        return  provinceService.getAreaListByCityCode(cityCode);
    }

    @ApiOperation(value = "获取省下面地级市列表")
    @GetMapping("/{provinceCode}/non-county-city")
    public ResponseData getNonCountyCityByProvinceCode(@PathVariable String provinceCode) {
        return provinceService.getNonCountyCityByProvinceCode(provinceCode);
    }


    @ApiOperation(value = "新增城市")
    @PostMapping
    public ResponseData addCity(@RequestBody  CmCity city) {
        ResponseData responseData = new ResponseData();
        if (!provinceService.checkCityAdd(city, responseData)) {
            return ResponseData.andaError(responseData.getMessage());
        }
        provinceService.addCity(city);
        return ResponseData.andaSuccess();
    }


    @ApiOperation(value = "城市删除")
    @DeleteMapping("/{code}")
    public ResponseData deleteCity(@PathVariable String code) {
        provinceService.deleteCity(code);
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "城市详情")
    @GetMapping("/{code}")
    public ResponseData getCityByCode(@PathVariable String code) {
        CmCity city = provinceService.getCityByCode(code);
        return ResponseData.andaSuccess(city);
    }



    @ApiOperation(value = "城市编辑")
    @PutMapping
    public ResponseData updateCity(@RequestBody @Validated CmCity city) {
        provinceService.updateCity(city);
        return ResponseData.andaSuccess();
    }



    @ApiOperation(value = "非县级市区新增")
    @PostMapping("/area")
    public ResponseData addArea(@RequestBody CmArea area) {
        String message = "";
        if (!provinceService.checkAreaAdd(area, message)) {
            return ResponseData.andaError(ReturnCode.RETURN_PARAM_ERROR.code,message);
        }
        provinceService.addArea(area);
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "非县级市区编辑")
    @PutMapping("/area")
    public ResponseData updateArea(@RequestBody @Validated CmArea area) {
        String andaErrorMsg = "";
        if (!provinceService.checkAreaUpdate(area, andaErrorMsg)) {
            return ResponseData.andaError(ReturnCode.RETURN_PARAM_ERROR.code,andaErrorMsg);
        }
        provinceService.updateArea(area);
        return ResponseData.andaSuccess();
    }

    @ApiOperation(value = "区详情")
    @GetMapping("/area/{code}")
    public ResponseData getAreaDetail(@PathVariable String code) {
        CmArea area = provinceService.getAreaByCode(code);
        return ResponseData.andaSuccess(area);
    }


    @ApiOperation(value = "获取所有区")
    @GetMapping("/area")
    public ResponseData getAreaList() {
        List list = provinceService.getAllAreaList();
        return ResponseData.andaSuccess(list);
    }

    @ApiOperation(value = "获取省份列表")
    @GetMapping("/province")
    public ResponseData getProvinceList() {
        List<CmProvince> list = provinceService.getProvinceList();
        return ResponseData.andaSuccess(list);
    }


    //无需登录
    @ApiOperation(value = "根据省->市展示城市列表")
    @GetMapping("/province-city")
    public ResponseData getPkgProvinceCity() {
        List<PkgProvince> list = provinceService.getPkgProvinceCity();
        return ResponseData.andaSuccess(list);
    }

    @ApiOperation(value = "删除区")
    @DeleteMapping("/area/{code}")
    public ResponseData deleteArea(@PathVariable String code) {
        provinceService.deleteArea(code);
        return ResponseData.andaSuccess();
    }
}
